package com.fyyg.sharding;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

public final class MonthShardingStrategy implements SingleKeyTableShardingAlgorithm<Integer> {

    @Override
    public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
        for (String each : availableTargetNames) {
            if (each.endsWith(shardingValue.getValue()+"")) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<String> doInSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
        Collection<Integer> values = shardingValue.getValues();
        for (Integer value : values) {
            for (String tableNames : availableTargetNames) {
                if (tableNames.endsWith(value+ "")) {
                    result.add(tableNames);
                }
            }
        }
        return result;
    }

    @Override
    public Collection<String> doBetweenSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
        Range<Integer> range = shardingValue.getValueRange();
        List<Integer> rangeList = rangeMonthYeahList(range.lowerEndpoint(),range.upperEndpoint());
        for (Integer value : rangeList) {
            for (String tableNames : availableTargetNames) {
                if (tableNames.endsWith(value+ "")) {
                    result.add(tableNames);
                }
            }
        }
        return result;
    }

    private List<Integer> rangeMonthYeahList(Integer rangeStart,Integer rangeEnd){
        List<Integer> list = new ArrayList<>();
        if(rangeStart == rangeEnd) {
            list.add(rangeEnd);
            return list;
        }
        Integer startYear = Integer.parseInt(rangeStart.toString().substring(0,2));
        Integer startMonth = Integer.parseInt(rangeStart.toString().substring(2));
        Integer endYear = Integer.parseInt(rangeEnd.toString().substring(0,2));
        Integer endMonth = Integer.parseInt(rangeEnd.toString().substring(2));
        if(startYear == endYear){
            int diffMonth = endMonth - startMonth;
            for (int i = 0; i <= diffMonth; i++) {
                int month = startMonth+i;
                String ym = month < 10 ? startYear+"0"+month : startYear+""+month;
                list.add(Integer.valueOf(ym));
            }
        }else if(endYear > startYear ){
            int diffYear = endYear - startYear;
            int sym = Integer.valueOf(rangeStart);
            int eym = Integer.valueOf(rangeEnd);
            for (int i = 0; i <= diffYear; i++) {
                int year = startYear + i;
                for (int k = 1; k <= 12; k++) {
                    String ym = k < 10 ? year+"0"+k : year+""+k;
                    if(Integer.parseInt(ym) > eym || Integer.parseInt(ym) < sym){
                        continue;
                    }
                    list.add(Integer.valueOf(ym));
                }
            }
        }else {
            throw new RuntimeException("between分片参数错误。。。。");
        }
        return list;
    }
}
